package org.fly.jingwei.ext.utils;

import cn.hutool.core.util.StrUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * @author leo.jie (weixiao.me@aliyun.com)
 * @version 1.0
 * @organization bigdata
 * @website https://www.jielongping.com
 * @date 2020/5/30 12:11 下午
 * @since 1.0
 */
public class StringEscapeHelper {
    public static String charset = "utf-8";

    public static String encode(String string) {
        if (StrUtil.isBlank(string)) {
            return string;
        }
        try {
            String tmp = URLEncoder.encode(string, charset);
            return tmp.replaceAll("\\+", "%20");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return string;
        }
    }

    public static String decode(String string) {
        if (StrUtil.isBlank(string)) {
            return string;
        }
        try {
            return URLDecoder.decode(string, charset);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return string;
        }
    }
}
